#include <io.h>
#include <signal.h>

/*
  Program to measure interrupt timings of the MSP430 board

  Finds an *upper limit* for the time it takes to enter an interrupt procedure

  Port 2 output is to be connected to Port 1 input
  Port 3 output is to be read by logic analyser

  By Pete R. Hague

  November 2009
*/

//Define sampling routine
int adcSample(char inchx, char srefx)
{
  int output;

  ADC12CTL0 |= (ADC12ON);

  ADC12MCTL0 = ((srefx<<4)+inchx); 
  ADC12CTL0 |= (ENC | ADC12SC);
  while (!(ADC12IFG & 0x1));
  output = ADC12MEM0;

  ADC12CTL0 &= ~ENC;

  ADC12CTL0 &= ~(REFON | ADC12ON);

  return output;
}

// Define interrupt vector
interrupt(PORT1_VECTOR) trigger(void) {
  adcSample(1,2);

  // Set Port 3 to high
  P3OUT=255;

  // Remove triggering condition
  P2OUT=0;

  // Reset Interrupt
  P1IE=255;
  P1IES=0;
  P1IFG=0;
}

// Main loop
int main() {
  int i;

  //P1DIR=0;
  //P1SEL=0;
  P1IE=255;
  P1IES=0;
  P1IFG=0;

  P3DIR=255;
  P3OUT=0;

  P2DIR=255;
  P2OUT=0;  

  P6SEL |= 0x80; 
  P6DIR |= ~0x80;

  ADC12CTL0=0xAA40; 
  ADC12CTL1=0x0200;
  ADC12IE=1; 

  eint();

  while(1) {
    // Set logic output low
    P3OUT=0;

    // Trigger Interrupt
    P2OUT=255;    

    // Waits to try interrupt again
    for (i=0;i<10000;i++)
      nop();
  }
}
